package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;

import org.dromara.common.mybatis.core.page.TableDataInfo;


import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.DmNameFile;
import org.dromara.work.domain.bo.DmNameFileSaveBo;
import org.dromara.work.domain.vo.DmNameFileVo;
import org.dromara.work.service.IDmNameFileService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 命名文件
 *
 * @author Chen
 * @date 2024-01-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/dm/name/file")
public class DmNameFileController extends BaseController {

    private final IDmNameFileService infoService;

    /**
     * 查询命名文件列表
     */
    @GetMapping("/list")
    @SaCheckPermission("dm:file:list")
    public TableDataInfo<DmNameFileVo> list(DmNameFileSaveBo bo, PageQuery pageQuery) {
        return infoService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取命名文件详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    @SaCheckPermission("dm:file:list")
    public R<DmNameFileVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(infoService.queryById(id));
    }

    /**
     * 获取命名文件详细信息
     *
     * @param id 主键
     */
    @GetMapping("/getFile/{id}")
    @SaCheckPermission("dm:file:list")
    public TableDataInfo<DmNameFileVo> getFile(@NotNull(message = "编码不能为空")
                                   @PathVariable String id) {
        return infoService.selectDmFileByCode(id);
    }

    /**
     * 新增命名文件信息
     */
    @RepeatSubmit()
    @PostMapping()
    @SaCheckPermission("dm:file:add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DmNameFileSaveBo bo) {
        return toAjax(infoService.insertByBo(bo));
    }

    /**
     * 修改命名文件信息
     */
    @RepeatSubmit()
    @PutMapping()
    @SaCheckPermission("dm:file:edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DmNameFileSaveBo bo) {
        return toAjax(infoService.updateByBo(bo));
    }

    /**
     * 删除命名文件信息
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    @SaCheckPermission("dm:file:remove")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(infoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
